package com.example.newstudent.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Component
public class lesson {
    int id;
    String name;
    String teacher;
    int sectioncount;
    String studentid;
    int section;
    int day;
    String weeks;
    String address;
    String color;
    int status;
    public void setAll(optional_course optional_course,String id){
        Random random = new Random();
        StringBuilder color = new StringBuilder("#");

        for (int i = 0; i < 6; i++) {
            // 生成一个0-15之间的随机数
            int hexDigit = random.nextInt(16);

            // 将其转换为十六进制表示形式
            if (hexDigit < 10) {
                color.append(hexDigit);
            } else {
                char letter = (char) ('A' + (hexDigit - 10));
                color.append(letter);
            }
        }
        this.color = color.toString();
        this.name = optional_course.name;
        this.teacher = optional_course.teacher;
        this.sectioncount = optional_course.sectioncount;
        this.day = optional_course.day;
        this.section = optional_course.section;
        this.weeks = optional_course.weeks;
        this.studentid = id;
        this.address = optional_course.address;
        this.status = 2;
    }
    public static Map<String, Map<String, Object>> summarizecourses(List<lesson> courses) {
        Map<String, Map<String, Object>> courseSummary = new HashMap<>();

        for (lesson course : courses) {
            // 如果map中没有这个课程名，就创建一个新的map来存储信息
            courseSummary.putIfAbsent(course.name, new HashMap<>());
            Map<String, Object> courseInfo = courseSummary.get(course.name);

            // 假设同一课程名的课程和地址都是相同的，只取第一个即可
            if (courseInfo.isEmpty()) {
                // 初始化周数为一个集合来避免重复
                courseInfo.put("weeks", new HashSet<String>(Arrays.asList(course.weeks.split(","))));
                // 存储老师和地址
                courseInfo.put("teacher", course.teacher);
                courseInfo.put("address", course.address);
            }
            // 如果不是第一个，则只需要更新周数集合（如果需要的话）
            else {
                // 合并周数集合
                Set<String> weeksSet = (Set<String>) courseInfo.get("weeks");
                weeksSet.addAll(Arrays.asList(course.weeks.split(",")));
            }
        }

        // 将周数集合转换回逗号分隔的字符串
        for (Map.Entry<String, Map<String, Object>> entry : courseSummary.entrySet()) {
            Set<String> weeksSet = (Set<String>) entry.getValue().get("weeks");
            entry.getValue().put("weeks", String.join(",", weeksSet));
        }

        return courseSummary;
    }
}
